﻿// CLoginState.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace Casters.source.States
{
    public class CLoginState : CBaseState
    {
        protected string m_ScreenName;
        protected Rectangle m_rScreenName;
        protected string m_Password;
        protected Rectangle m_rPassword;
        protected Vector2 m_CameraOffset;
        protected bool m_bScreenNameFocused;
        protected SpriteFont m_Font;

        // Debug info
        protected Texture2D pixel;

        static private CLoginState instance = null;
        public static CLoginState GetInstance()
        {
            if (instance == null)
                instance = new CLoginState();
            
            return instance;
        }

        CLoginState() { }

        /// <summary>
        /// Setup the state
        /// </summary>
        public override void Initialize()
        {
            m_ScreenName = "";
            m_Password = "";

            m_rScreenName = new Rectangle((int)(CGame.GetInstance().viewPort.X * 0.5f - 40f), (int)(CGame.GetInstance().viewPort.Y * 0.5f), 100, 20);
            m_rPassword = new Rectangle((int)(CGame.GetInstance().viewPort.X * 0.5f - 40f), (int)(CGame.GetInstance().viewPort.Y * 0.5f + 20f), 100, 20);

            m_bScreenNameFocused = true;

            m_CameraOffset = CGame.GetInstance().m_Camera.m_vPosition;
            m_rScreenName.X += (int)m_CameraOffset.X;
            m_rScreenName.Y += (int)m_CameraOffset.Y;
            m_rPassword.X += (int)m_CameraOffset.X;
            m_rPassword.Y += (int)m_CameraOffset.Y;
        }

        /// <summary>
        /// Load all resources needed for this state
        /// </summary>
        public override void LoadContent()
        {
            // Debug stuff
            pixel = CGame.GetInstance().Content.Load<Texture2D>("pixel");

            m_Font = CGame.GetInstance().Content.Load<SpriteFont>("Fonts/Arial");
            CEventSystem.RegisterEventHandler(EventID.MouseLeftClick, OnMouseLeftClick);
            CEventSystem.RegisterEventHandler(EventID.Confirm, OnConfirm);

            base.LoadContent();
        }

        /// <summary>
        /// Unload anything needed before exiting this state
        /// </summary>
        public override void UnloadContent()
        {
            CEventSystem.UnregisterEventHandler(EventID.MouseLeftClick, OnMouseLeftClick);
            CEventSystem.UnregisterEventHandler(EventID.Confirm, OnConfirm);

            base.UnloadContent();
        }

        /// <summary>
        /// Run update logic for this state
        /// </summary>
        /// <param name="dt">Delta Time</param>
        public override void Update(float dt)
        {
            Keys[] keys = CKeyboardManager.GetPressedKeys();

            foreach(Keys key in keys)
            {
                if (CKeyboardManager.KeyPressed(key))
                {
                    switch (key)
                    {
                        case Keys.A:
                        case Keys.B:
                        case Keys.C:
                        case Keys.D:
                        case Keys.E:
                        case Keys.F:
                        case Keys.G:
                        case Keys.H:
                        case Keys.I:
                        case Keys.J:
                        case Keys.K:
                        case Keys.L:
                        case Keys.M:
                        case Keys.N:
                        case Keys.O:
                        case Keys.P:
                        case Keys.Q:
                        case Keys.R:
                        case Keys.S:
                        case Keys.T:
                        case Keys.U:
                        case Keys.V:
                        case Keys.W:
                        case Keys.X:
                        case Keys.Y:
                        case Keys.Z:
                            if (m_bScreenNameFocused)
                                m_ScreenName += key.ToString();
                            else
                                m_Password += key.ToString();
                            break;
                        case Keys.Back:
                            if (m_bScreenNameFocused && m_ScreenName.Length > 0)
                                m_ScreenName = m_ScreenName.Remove(m_ScreenName.Length - 1);
                            else if (m_Password.Length > 0)
                                m_Password = m_Password.Remove(m_Password.Length - 1);
                            break;
                    }
                }
            }

            base.Update(dt);
        }

        /// <summary>
        /// Draw stuff to the screen
        /// </summary>
        public override void Draw()
        {
            CRenderEngine.DrawString(m_Font, "Focus: " + m_bScreenNameFocused.ToString(), new Vector2(100f, 100f) - m_CameraOffset, Color.White);

            CRenderEngine.Draw(pixel, m_rScreenName, Color.Pink);
            CRenderEngine.Draw(pixel, m_rPassword, Color.Green);

            int index = 0;
            string tempStr = "";
            if (10 < m_ScreenName.Length)
            {
                index = (m_ScreenName.Length) - 10;
                tempStr = m_ScreenName.Remove(0, index);
                CRenderEngine.DrawString(m_Font, "Screen Name: " + tempStr, new Vector2(m_rScreenName.X, m_rScreenName.Y), Color.White);
            }
            else
                CRenderEngine.DrawString(m_Font, "Screen Name: " + m_ScreenName, new Vector2(m_rScreenName.X, m_rScreenName.Y), Color.White);

            index = 0;
            tempStr = "";
            if (10 < m_Password.Length)
            {
                index = m_Password.Length - 10;
                tempStr = m_Password.Remove(0, index);
                CRenderEngine.DrawString(m_Font, "Password: " + tempStr, new Vector2(m_rPassword.X, m_rPassword.Y), Color.White);
            }
            else
                CRenderEngine.DrawString(m_Font, "Password: " + m_Password, new Vector2(m_rPassword.X, m_rPassword.Y), Color.White);
        }

        public void OnMouseLeftClick(params object[] eventArgs)
        {
            Point mousePos = new Point((int)((Vector2)eventArgs[0]).X, (int)((Vector2)eventArgs[0]).Y);

            if (m_rScreenName.Contains(mousePos))
                m_bScreenNameFocused = true;
            else if (m_rPassword.Contains(mousePos))
                m_bScreenNameFocused = false;
        }

        public void OnConfirm(params object[] eventArgs)
        {
            CEventSystem.BroadcastEvent(EventID.ChangeState, CMenuState.GetInstance());
        }
    }
}
